package com.hxzy.dao.impl;

import com.hxzy.bean.HandSet;
import com.hxzy.dao.HandSetDao;
import com.hxzy.util.JdbcUtils;

import java.util.List;

/**
 * @author nick
 */
public class HandSetDaoImpl implements HandSetDao {
    @Override
    public int save(HandSet handSet) {
        String sql = "INSERT INTO handset(name_type,price,network_mode,network_type,facade,screen_size,feel_screen) values (?,?,?,?,?,?,?)";
        Object[] params = {handSet.getNameType(), handSet.getPrice(), handSet.getNetworkMode(), handSet.getNetworkType()
                , handSet.getFacade(), handSet.getScreenSize(), handSet.getFeelScreen()};
        return JdbcUtils.executeUpdate(sql,params);
    }

    @Override
    public int update(HandSet handSet) {
        String sql = "UPDATE handset set name_type=?,price=?,network_mode=?,network_type=?,facade=?,screen_size=?,feel_screen=? where hs_id=?";
        Object[] params = {handSet.getNameType(), handSet.getPrice(), handSet.getNetworkMode(), handSet.getNetworkType()
                , handSet.getFacade(), handSet.getScreenSize(), handSet.getFeelScreen(),handSet.getHsId()};
        return JdbcUtils.executeUpdate(sql,params);
    }

    @Override
    public int remove(Integer integer) {
        String sql = "delete from handset where hs_id=?";

        return JdbcUtils.executeUpdate(sql,integer);
    }

    @Override
    public List<HandSet> queryAll() {
        String sql = new StringBuilder()
                .append("select ")
                .append(" hs_id as hsId,")
                .append(" name_type as nameType,")
                .append(" price as price,")
                .append(" network_mode as networkMode,")
                .append(" network_type as networkType,")
                .append(" facade as facade,")
                .append(" screen_size as screenSize,")
                .append(" feel_screen as feelScreen")
                .append(" from handset").toString();
        return JdbcUtils.selectSingleTab(HandSet.class,sql);
    }

    @Override
    public HandSet findById(Integer integer) {
        String sql = new StringBuilder()
                .append("select ")
                .append(" hs_id as hsId,")
                .append(" name_type as nameType,")
                .append(" price as price,")
                .append(" network_mode as networkMode,")
                .append(" network_type as networkType,")
                .append(" facade as facade,")
                .append(" screen_size as screenSize,")
                .append(" feel_screen as feelScreen")
                .append(" from handset")
                .append(" where hs_id=?").toString();
        List<HandSet> handSets = JdbcUtils.selectSingleTab(HandSet.class, sql, integer);
        return handSets.size() > 0 ? handSets.get(0) : null;
    }
}
